package com.superjson.showpro.service.impl;

import com.superjson.showpro.base.GenericServiceImpl;
import com.superjson.showpro.dao.ArticleCatalogDao;
import com.superjson.showpro.entity.ArticleCatalog;
import com.superjson.showpro.service.ArticleCatalogService;
import com.superjson.showpro.shared.result.Result;
import com.superjson.showpro.shared.result.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.beantools.util.ObjectUtil.isNotNull;
import static com.superjson.showpro.shared.util.CommonUtil.getQuillContent;

/**
 * Created By zby on 23:36 2019/5/28
 */
@Service
public class ArticleCatalogServiceImpl extends GenericServiceImpl<ArticleCatalog> implements ArticleCatalogService {

    private ArticleCatalogDao articleCatalogDao;

    @Autowired
    public ArticleCatalogServiceImpl(ArticleCatalogDao articleCatalogDao) {
        super(articleCatalogDao);
        this.articleCatalogDao = articleCatalogDao;
    }

    @Override
    public Result saveAc(ArticleCatalog articleCatalog, Long parentId) {
        // 【1】保存父目录
        articleCatalog.setParent(isNotNull(parentId) && parentId > 0 ? articleCatalogDao.get(parentId) : null);
        //【2】保存序号
        Integer maxSort = articleCatalogDao.getMaxSort();
        articleCatalog.setSort(maxSort + 10);
//        保存路径
        articleCatalog = articleCatalogDao.save(articleCatalog);
        articleCatalog.setContent(getQuillContent(articleCatalog.getContent()));
        ArticleCatalog dbParent = articleCatalog.getParent();
        articleCatalog.setFullIdPath((isNotNull(dbParent) ? dbParent.getFullIdPath() + "-" : "") + articleCatalog.getId());
        articleCatalogDao.save(articleCatalog);
        return ResultUtil.buildSuccess();
    }

    @Override
    public Result<List<ArticleCatalog>> list(boolean nullParent) {
        return ResultUtil.buildSuccess(articleCatalogDao.list(nullParent));
    }

    @Override
    public Result<List<ArticleCatalog>> listByParent(Long parentId) {
        return ResultUtil.buildSuccess(articleCatalogDao.listByParent(parentId));
    }


}